package com.github.lee.mvvm.core.vm

import androidx.annotation.IntRange
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.MutableLiveData
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder


abstract class BaseRefreshViewModel<T,
        A : BaseQuickAdapter<T, *>> : BaseViewModel() {
    protected var currentPage = 1

    val refreshLiveData = MutableLiveData(false)
    val contentAdapter: A by lazy { onCreateAdapter() }
    val data
        get() = contentAdapter.data


    protected abstract fun onCreateAdapter(): A

    //===Desc:=========================================================


    protected fun setNewData(list: List<T>) {
        contentAdapter.setNewInstance(list.toMutableList())
    }

    protected fun addData(data: T, @IntRange(from = 0) position: Int? = null) {
        if (null == position) {
            contentAdapter.addData(data)
        } else {
            contentAdapter.addData(position, data)
        }
    }

    protected fun addData(data: List<T>, @IntRange(from = 0) position: Int? = null) {
        if (null == position) {
            contentAdapter.addData(data)
        } else {
            contentAdapter.addData(position, data)
        }
    }

    protected fun remove(data: T) {
        contentAdapter.remove(data)
    }

    protected fun removeAt(@IntRange(from = 0) position: Int) {
        contentAdapter.removeAt(position)
    }

    //===Desc:=========================================================

    protected fun stopRefresh(){
        refreshLiveData.postValue(false)
    }

    protected fun loadMoreComplete() {
        if (contentAdapter is LoadMoreModule) {
            contentAdapter.loadMoreModule.loadMoreComplete()
        }
    }

    protected fun loadMoreEnd(gone: Boolean = false) {
        if (contentAdapter is LoadMoreModule) {
            contentAdapter.loadMoreModule.loadMoreEnd(gone)
        }
    }

    protected fun loadMoreFail() {
        if (contentAdapter is LoadMoreModule) {
            contentAdapter.loadMoreModule.loadMoreFail()
        }
    }


}